// $.get("header_menu.html",function(data){ $("#header").html(data); });
$.get("footer.html",function(data){ $("#footer").html(data); });

$(document).ready(function() {
    // $("#active").addClass("active");
    // $("#active1").addClass("active");

    getData(1);
    $('.check1').click(function () {
        getData(1);
    });

    function getData(curr) {
        var keyword = $('#search_input').val();
        $.ajax({
            type: "get",
            url: "http://114.116.72.220:9999/PcShowActivity",
            async: true,
            data: {
                keyword: keyword,
                page: curr,
                row: 5
            },
            success: function (data) {
                if (data[0].length == 0) {
                    layer.msg('暂无数据');
                }
                pageDivd(curr, data[1]);
                TableView(data[0]);
            }
        });
    }
    function pageDivd(curr, page_num) {
        layui.use(['laypage', 'layer'], function () {
            var laypage = layui.laypage
                , layer = layui.layer;
            laypage.render({
                elem: 'demo7'
                , count: page_num
                , limit: 5
                , curr: curr
                , jump: function (obj, first) {
                    if (!first) {
                        getData(obj.curr);
                    }
                }
            });
        });
    }

    function TableView(data) {
        $('#act_detail_table').html('');
        for (var i = 0; i < data.length; i++) {
            var trText = `<tr  class="${i % 2 ? '' : 'tr_back'}">
					<td>${i + 1}</td>
					<td>
						<img src="${data[i].activity_pic}" height="60px"/>
					</td>
					<td>${data[i].activity_no}</td>
					<td>${data[i].activity_city}</td>
					<td>
						${data[i].activity_name}
					</td>
					<td>
						${data[i].content}
					</td>
				
					<td index="${data[i].activity_id}">
						<button class="look_detail">查看</button>
						<button class="applicant">报名人数</button>
						<button class="kind_del">结束</button>
					</td>
				</tr>`;

            $('#act_detail_table').append(trText);

        }
        data_edtil();
    }

    function data_edtil() {
        $('.look_detail').click(function () {
            var Index = $(this).parent().attr('index');
            window.location.href = "http://114.116.72.220:9010/src/active_detail.html?Index=" + Index;
        });
        $('.applicant').click(function () {
            var Index = $(this).parent().attr('index');
            window.location.href = "http://114.116.72.220:9010/src/active_applicant.html?Index=" + Index;
        });
        $('.kind_del').click(function () {
            var Index = $(this).parent().attr('index');
            layer.confirm('确定要结束该活动吗？', {
                title: ['提示'],
                btn: ['确定', '取消'] //按钮
            }, function () {
                $.ajax({
                    type: "get",
                    url: "http://114.116.72.220:9999/EndActivity",
                    async: true,
                    data: {
                        activityId: Index,
                    },
                    success: function (data) {
                        if (data == 'success') {
                            layer.msg('删除成功', {time: 500}, function () {
                                location.reload()
                            })
                        } else {
                            layer.msg('删除失败', {time: 500})
                        }
                    }
                });
            });
        });
    }

});